package com.mcg.jituan;

import com.alibaba.fastjson.JSONObject;
import com.hl.opnc.AbilityResponse;
import com.hl.opnc.ResponseBody;
import com.hl.opnc.invoke.AbilityInvokeManager;
import com.hl.opnc.invoke.InvokeManager;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.mcg.jituan.bean.Duixiang;
import com.mcg.jituan.bean.Results;


public class post {
    public static void main(String[] args) throws Exception {
//        String body= "{\"f_commands\": ['REG NE:NAME=\"LLFSHFH16QQT1FF_方山全顺达洗煤厂\";','LST ESN:;','DSP LICINFO:;','CHK DATA2LIC:;','DSP RVKLICINFO:;'],\"f_connection_id\": \"NULL\",\"f_serviceid\":3083,\"f_trailing\": 0}";
//        System.out.println(post(body));
        Map<String, List<String>> map = new LinkedHashMap<>();
        String path = "D:\\ash\\jituan.txt";
        String string = post(json(readTxt(path)));
        JSONObject json = JSONObject.parseObject(string);
        Duixiang duixiang = JSON.toJavaObject(json, Duixiang.class);
        List<Results> list = duixiang.getBody().getF_results();
        for (Results s : list) {
            String key = s.getF_command().split(":")[0];
            if (!map.containsKey(key)) {
                List<String> reslist = new ArrayList<>();
                reslist.add(s.getF_result());
                map.put(key, reslist);
            } else {
                List<String> ll = map.get(key);
                ll.add(s.getF_result());
                map.put(key, ll);
            }

        }
    }

    public static Map<String,List<String>> mmm(String path) throws Exception {

        Map<String, List<String>> map = new LinkedHashMap<>();
        String string = post(json(readTxt(path)));
        JSONObject json = JSONObject.parseObject(string);
        Duixiang duixiang = JSON.toJavaObject(json, Duixiang.class);
        List<Results> list = duixiang.getBody().getF_results();
        for (Results s : list) {
            String key = s.getF_command().split(":")[0];
            if (!map.containsKey(key)) {
                List<String> reslist = new ArrayList<>();
                reslist.add(s.getF_result());
                map.put(key, reslist);
            } else {
                List<String> ll = map.get(key);
                ll.add(s.getF_result());
                map.put(key, ll);
            }
        }
        return map;
    }

    public static String post(String json) throws IOException {
        String url = "https://172.25.34.180/api/middlleplatform/HmbIix/59Awtc/v1.0";
        InvokeManager manager = new AbilityInvokeManager("666a12daaccd4c19a077ff027d8f1aa6", "9349c9639dac44e4a61d92299a463e75");
        AbilityResponse response = manager.post(url, null, json);
        ResponseBody respObj = response.getRespObj();
        response.close();
        return respObj.getUNI_NET_BODY().toString();
    }

    public static List<String> readTxt(String txtPath) {
        File file = new File(txtPath);
        if (file.isFile() && file.exists()) {
            try {
                List<String> list = new ArrayList<>();
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                String text = null;
                while ((text = bufferedReader.readLine()) != null) {
                    list.add(text);
                }
                return list;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String json(List<String> list) {
        String a = "{\"f_commands\": [";
        String b = "],\"f_connection_id\": \"NULL\",\"f_serviceid\":3083,\"f_trailing\": 0}";
        StringBuilder sb = new StringBuilder();
        sb.append(a);
        for (String s : list) {
            sb.append("'");
            sb.append(s);
            sb.append("'");
            if (!(s.equals(list.get(list.size() - 1))))
                sb.append(",");
        }
        sb.append(b);
        return sb.toString();
    }


}



